var express = require('express');
var bodyParser = require('body-parser');
var db = require('./fengzhuang2.js')
var app = express();
var ObjectId = require('mongodb').ObjectID;
var formidable = require('formidable'); //提供上传文件的模块
var fs=require('fs');   //提供更改名字模块
var path = require('path'); //提供获取后缀名的方法
app.use(bodyParser.urlencoded({extended:false}))
app.use('/reg',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var username = req.body.username;
    var password = req.body.password;
    var getemail=new Array();
    var userlist=new Array();
    var data={
        username:username,
        password:password,
        getemail:getemail,
        userlist:userlist,
    }
    var kong={};
    var arrname=[];
    db.find('user',kong,function(err,result){
        for(var i in result){
            arrname.push(result[i].username)
        }
        if(arrname.indexOf(username)==-1){
            db.insertOne('user',data,function(err,result){
                if(err){
                    console.log(err)
                }
                res.send('success');
            })
        }else{
            res.send('error');
        }
    })

})
app.use('/log',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var username = req.body.username;
    var password = req.body.password;
    var data={
        username:username,
        password:password
    }
    db.find('user',data,function(err,result){
        if(err){
            console.log(err)
        }
        if(result!=""){
            res.send('success');
        }else{
            res.send('error')
        }

    })
})
app.use('/send',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    let recipients = req.body.recipients;
    let titleinput = req.body.titleinput;
    let theme = req.body.theme;
    var sendtorname =  req.body.sendtor;
    var names={
        username:recipients
    }
    var data={
        title:titleinput,
        theme:theme,
        sendtorname:sendtorname,
    }
    db.find('user',names,function(err,result){
        if(result==""){
            res.send('nopeople');
        }else{
            if(sendtorname==recipients){
                res.send('nopeople');
            }else{
                result[0].getemail.push(data);
                db.updateMany(
                    "user",
                    {
                        username:recipients
                    },
                    {
                        $set:{ getemail: result[0].getemail}     //怎么改
                    },
                    function(err,result){   //改完之后做什么
                        if(err){
                            console.log(err);
                        }
                        res.send('success');
                    }
                );

            }

        }
    })

})
app.use('/inboxlength',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var username = req.body.username;
    var data={
        username:username
    }
    db.find('user',data,function(err,result){
        if(err){console.log(err)}
        var cd = String(result[0].getemail.length);
        res.send(cd);
    })
})
app.use('/inboxtheme',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var username = req.body.username;
    var data={
        username:username
    }
    db.find('user',data,function(err,result){
        if(err){console.log(err)}
        res.send(result);
    })
})
app.use('/emailtext',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var username = req.body.username;
    var index = req.body.index;
    var data={
        username:username,
    }
    db.find('user',data,function(err,result){
        if(err){console.log(err)}
        res.send(result[0].getemail[index].theme);
    })
})
app.use('/forname',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var username = req.body.username;
    var index = req.body.index;
    var data={
        username:username,
    }
    db.find('user',data,function(err,result){
        if(err){console.log(err)}
        res.send(result[0].userlist[index]);
    })
})
app.use('/adduser',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var username = req.body.username;
    var localname = req.body.localname;

    var data={
        username:localname,
    }
    var kong={
        username:username,
    }
    db.find('user',kong,function(err,result){
        if(result==""){
            res.send('kong')
        }else{
            db.find('user',data,function(err,result){
                if(err){console.log(err)}
                if(localname==username){
                    res.send('equal');
                }else{
                    if(result[0].userlist.indexOf(username)==-1){
                        result[0].userlist.push(username);
                        db.updateMany(
                            "user",
                            {
                                username:localname
                            },
                            {
                                $set:{ userlist: result[0].userlist}     //怎么改
                            },
                            function(err,result){   //改完之后做什么
                                if(err){
                                    console.log(err);
                                }
                                res.send('success');
                            }
                        );
                    }else{
                        res.send('error');
                    }
                }
            })
        }
    })

})

app.listen(8080);
